import 'package:flutter/material.dart';
import 'controller.dart';

class KeyboardSpace extends StatefulWidget {
  final Widget child;
  final bool enable;
  final Duration duration;
  final double paddingBottom;

  KeyboardSpace(
      {required this.child, this.enable = true,this.paddingBottom=0, this.duration = const Duration(
          milliseconds: 300)});

  @override
  _KeyboardSpaceState createState() => _KeyboardSpaceState();
}

class _KeyboardSpaceState extends State<KeyboardSpace>
    with WidgetsBindingObserver, SingleTickerProviderStateMixin {

  KeyboardSpaceController c = new KeyboardSpaceController();

  @override
  void initState() {
    WidgetsBinding.instance.addObserver(this);
    c.registerState(this);
    c.duration = widget.duration;
    c.registerAnimationController(AnimationController(vsync: this));
    super.initState();
  }

  @override
  void didChangeMetrics() async {
    super.didChangeMetrics();
    if (widget.enable) {
      c.didChangeMetrics();
    }
  }

  @override
  void dispose() {
    WidgetsBinding.instance.removeObserver(this);
    c.animationController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    var bottom = MediaQuery
        .of(context)
        .viewInsets
        .bottom;
    // print("transY:${bottom}");
    return Stack(children: [
      Positioned.fill(
          child: Transform.translate(offset: Offset(0, c.transformY),
              child: Container(child: widget.child,)
          )),
      // if(bottom > 30)
      //   Positioned.fill(child: GestureDetector(onTap:c.onSpecClick,child:Container(color:Color(0x33ffffff))))
      //

    ]
      ,
    );
  }
}
